<?php

namespace app\merchant\model;

use app\common\model\Coupon as CouponModel;

/**
 * 优惠券模型
 * Class Coupon
 * @package app\store\model
 */
class Coupon extends CouponModel
{
    /**
     * 获取优惠券列表
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getList()
    {
        return $this->where('is_delete', '=', 0)
            ->order(['sort' => 'asc', 'create_time' => 'desc'])
            ->paginate(15, false, [
                'query' => request()->request()
            ]);
    }

    /**
     * 表单验证
     * @param $data
     * @param string $scene
     * @return bool
     */
    private function onValidate($data)
    {

        if (!isset($data['name']) || empty($data['name'])) {
            $this->error = '请输入优惠券名称';
            return false;
        }
        if (!isset($data['color']) || isInt($data['color'])) {
            $this->error = '请选择优惠券颜色';
            return false;
        }
        if (!isset($data['coupon_type']) || isInt($data['coupon_type'])) {
            $this->error = '请选择优惠券类型';
            return false;
        }
        //优惠券类型(10满减券 20折扣券)
        if($data['coupon_type']==10){
            if (!isset($data['reduce_price']) || isInt($data['reduce_price'])) {
                $this->error = '请输入正确的减免金额';
                return false;
            }
        }

        if($data['coupon_type']==20){
            if (!isset($data['discount']) || isZeroNumeric($data['discount']) || $data['discount']>10) {
                $this->error = '请输入正确的折扣率';
                return false;
            }
        }

        if (!isset($data['min_price']) || isZeroNumeric($data['min_price'])) {
            $this->error = '请输入正确的最低消费金额';
            return false;
        }

        if (!isset($data['expire_type']) || isInt($data['expire_type'])) {
            $this->error = '请选择到期类型';
            return false;
        }

        //到期类型(10领取后生效 20固定时间)
        if($data['expire_type']==10){
            if (!isset($data['expire_day']) || isInt($data['expire_day'])) {
                $this->error = '请输入正确的有效天数';
                return false;
            }
        }

        if($data['expire_type']==20){
            if (!isset($data['start_time']) || empty($data['start_time'])) {
                $this->error = '请选择开始日期';
                return false;
            }
            if (!isset($data['end_time']) || empty($data['end_time'])) {
                $this->error = '请选择结束日期';
                return false;
            }
            if (strtotime($data['start_time']) > strtotime($data['end_time'])) {
                $this->error = '开始日期不能大于结束日期';
                return false;
            }

        }


        if (!isset($data['total_num']) || !is_int($data['total_num']) || $data['total_num']<=-1) {
            $this->error = '请输入正确的发放总数量';
            return false;
        }


        if (!isset($data['sort']) || isZeroNumeric($data['sort'])) {
            $this->error = '请输入正确的排序';
            return false;
        }


        return true;
    }

    /**
     * 添加新记录
     * @param $data
     * @return false|int
     */
    public function add($data)
    {
        if (!$this->onValidate($data)) {
            return false;
        }
        $data['wxapp_id'] = self::$wxapp_id;
        if ($data['expire_type'] == '20') {
            $data['start_time'] = strtotime($data['start_time']);
            $data['end_time'] = strtotime($data['end_time']);
        }
        return $this->allowField(true)->save($data);
    }

    /**
     * 更新记录
     * @param $data
     * @return bool|int
     */
    public function edit($data)
    {
        if (!$this->onValidate($data)) {
            return false;
        }

        if ($data['expire_type'] == '20') {
            $data['start_time'] = strtotime($data['start_time']);
            $data['end_time'] = strtotime($data['end_time']);
        }
        return $this->allowField(true)->save($data) !== false;
    }

    /**
     * 删除记录 (软删除)
     * @return bool|int
     */
    public function setDelete()
    {
        return $this->save(['is_delete' => 1]) !== false;
    }

}
